import 'dart:convert';

import '../../tools_page/models/tool.dart';
import '../../../exercises/exercises_page/models/exercise.dart';

ExerciseTool exerciseToolFromJson(String str) => ExerciseTool.fromJson(json.decode(str));

String exerciseToolToJson(ExerciseTool data) => json.encode(data.toJson());

class ExerciseTool {
  final int exerciseToolId;
  final int exerciseId;
  final int toolId;
  final int active;
  final int isDeleted;
  final int createdBy;
  final int? updatedBy;
  final int? deletedBy;
  final DateTime createAt;
  final DateTime updateAt;
  final DateTime? deletedAt;
  final ToolModel tool;
  final Exercise exercise;

  ExerciseTool({
    this.exerciseToolId = -1,
    required this.exerciseId,
    required this.toolId,
    required this.active,
    required this.isDeleted,
    required this.createdBy,
    this.updatedBy,
    this.deletedBy,
    required this.createAt,
    required this.updateAt,
    this.deletedAt,
    required this.tool,
    required this.exercise,
  });

  ExerciseTool copyWith({
    int? exerciseToolId,
    int? exerciseId,
    int? toolId,
    int? active,
    int? isDeleted,
    int? createdBy,
    int? updatedBy,
    int? deletedBy,
    DateTime? createAt,
    DateTime? updateAt,
    DateTime? deletedAt,
    ToolModel? tool,
    Exercise? exercise,
  }) =>
      ExerciseTool(
        exerciseToolId: exerciseToolId ?? this.exerciseToolId,
        exerciseId: exerciseId ?? this.exerciseId,
        toolId: toolId ?? this.toolId,
        active: active ?? this.active,
        isDeleted: isDeleted ?? this.isDeleted,
        createdBy: createdBy ?? this.createdBy,
        updatedBy: updatedBy ?? this.updatedBy,
        deletedBy: deletedBy ?? this.deletedBy,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
        deletedAt: deletedAt ?? this.deletedAt,
        tool: tool ?? this.tool,
        exercise: exercise ?? this.exercise,
      );

  factory ExerciseTool.fromJson(Map<String, dynamic> json) => ExerciseTool(
    exerciseToolId: json["exercise_tool_id"],
    exerciseId: json["exercise_id"],
    toolId: json["tool_id"],
    active: json["active"],
    isDeleted: json["is_deleted"],
    createdBy: json["created_by"],
    updatedBy: json["updated_by"],
    deletedBy: json["deleted_by"],
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    deletedAt: DateTime.tryParse(json["deleted_at"].toString()),
    tool: ToolModel.fromJson(json["tool"]),
    exercise: Exercise.fromJson(json["exercise"]),
  );

  Map<String, dynamic> toJson() => {
    "gym_sport_id": exerciseToolId,
    "gym_id": exerciseId,
    "sport_id": toolId,
    "active": active,
    "is_deleted": isDeleted,
    "created_by": createdBy,
    "updated_by": updatedBy,
    "deleted_by": deletedBy,
    "create_at": createAt.toIso8601String(),
    "update_at": updateAt.toIso8601String(),
    "deleted_at": deletedAt,
    "sport": tool.toJson(),
    "gym": exercise.toJson(),
  };
}